$(function () {
    var checkDataGrid,checkDialog,bankAreaId,provinceId,cityId,checkForm,slotCardDialog,
        slotCardForm,bankAreaId1,provinceId1,cityId1,checkSlotCardAndCheckAndTransform,issueDialog,issueForm,
        transformDialog,transformForm,issueSendBack,orderBillForm;
    checkDataGrid = $("#check_datagrid");
    checkDialog = $("#check_dialog");
    bankAreaId = $('#bankAreaId');
    provinceId = $('#provinceId');
    cityId = $('#cityId');
    bankAreaId1 = $('#bankAreaId1');
    provinceId1 = $('#provinceId1');
    cityId1 = $('#cityId1');
    checkForm = $("#check_form");
    slotCardDialog = $("#slotCard_dialog");
    slotCardForm = $("#slotCard_form");
    issueDialog = $("#issue_dialog");
    issueForm = $("#issue_form");
    transformDialog = $("#transform_dialog");
    transformForm = $("#transform_form");
    orderBillForm = $("#orderBillForm");
    issueSendBack = $("#issueSendBack");
    checkSlotCardAndCheckAndTransform = $("#checkSlotCard,#checkCheck,#checkTransform");

    checkDataGrid.datagrid({
        fit:true,
        singleSelect:true,
        pagination:true,
        url:'/check/list',
        columns:[
            [
                {field:'ck',checkbox:true},
                {field:'ob_insureBillNumber',align:'center',width:180,title:'投保单号'},
                {field:'c_issue',align:'center',width:50,title:'期次'},
                {field:'cli_realname',align:'center',width:100,title:'投保人'},
                {field:'c_paymentType',align:'center',width:100,title:'缴费类型'},
                {field:'ins_payable',align:'center',width:100,title:'保费金额'},
                {field:'ins_payable',align:'center',width:100,title:'应缴金额'},
                {field:'ob_status',align:'center',width:100,title:'处理状态',formatter:statusFormatter},
                {field:'ob_beginDate',align:'center',width:140,title:'起保日期'},
                {field:'c_serialNumber',align:'center',width:180,title:'收据流水号'},

                {field:'ogn_organizationName',align:'center',width:100,title:'业务机构'},
                {field:'pd_name',align:'center',width:100,title:'产品'},
                {field:'c_underwritingform',align:'center',width:100,title:'申请类型'},
                {field:'ri_reviseid',align:'center',width:180,title:'保批单号'},
                {field:'c_cardNumber',align:'center',width:180,title:'卡号'},
                {field:'c_documentNumber',align:'center',width:180,title:'单证号'},
                {field:'c_chequeNo',align:'center',width:180,title:'支票号'},
                {field:'ba_name',align:'center',width:100,title:'银行省'},
                {field:'br_name',align:'center',width:100,title:'银行市'},
                {field:'bn_name',align:'center',width:100,title:'银行名称'},
                {field:'bk_name',align:'center',width:100,title:'支行名称'}
            ]
        ],
        onClickRow:function(rowIndex,rowData){
        //判断当前记录中的状态的值.
            if(rowData.ob_status==5){
                //该投保单已经缴费了,刷卡缴费和支票登记按钮应该变灰.
                checkSlotCardAndCheckAndTransform.linkbutton("disable");
            }else{
                //启用按钮
                checkSlotCardAndCheckAndTransform.linkbutton("enable");
            }
            if(rowData.ob_status==4){
                //该投保单未缴费了,见费出单退回按钮应该变灰.
                issueSendBack.linkbutton("disable");
            }else{
                //启用按钮
                issueSendBack.linkbutton("enable");
            }
        }
    });
    //高级查询
    $("a[data-query]").on("click",function(){
        //获取表单 高级查询条件, 封装到 qo中, 并且重新加载 datagrid
        var organizationNameD = $("[name=organizationNameD]").val();
        var aplicantName = $("[name=aplicantName]").val();
        var payableBeginC = $("[name=payableBeginC]").val();
        var payableEndC = $("[name=payableEndC]").val();
        var paymentTypeC = $("[name=paymentTypeC]").val();
        var paymentStatusC = $("[name=paymentStatusC]").val();
        var beginDateD = $("[name=beginDateD]").val();
        var endDateD = $("[name=endDateD]").val();
        var productName = $("[name=productName]").val();
        checkDataGrid.datagrid("load",{
            organizationNameD:organizationNameD,
            aplicantName:aplicantName,
            payableBeginC:payableBeginC,
            payableEndC:payableEndC,
            paymentTypeC:paymentTypeC,
            paymentStatusC:paymentStatusC,
            beginDateD:beginDateD,
            endDateD:endDateD,
            productName:productName
        });
    });
    //支票对话框
    checkDialog.dialog({
        width:750,
        height:440,
        buttons:'#check_dialog_bt',
        modal:true,
        closed:true
    });
    //刷卡对话框
    slotCardDialog.dialog({
        width:800,
        height:480,
        buttons:'#slotCard_dialog_bt',
        modal:true,
        closed:true
    });
    //查看对话框
    issueDialog.dialog({
        width:800,
        height:480,
        buttons:'#issue_dialog_bt',
        modal:true,
        closed:true
    });
    //缴费类型转换对话框
    transformDialog.dialog({
        width:800,
        height:530,
        buttons:'#transform_dialog_bt',
        modal:true,
        closed:true
    });
    //对按钮进行统一事件监听
    $("a[data-cmd]").on("click",function(){
        var cmd = $(this).data("cmd");
        if(cmd){
            cmdObj[cmd]();
        }
    });
    //方法统一管理起来
    var cmdObj = {
        transform:function () {
            var rowData = checkDataGrid.datagrid("getSelected");
            if(rowData){
                //1.清空表单数据
                transformForm.form("clear");
                //2.设置对话框的标题
                transformDialog.dialog("setTitle","缴费类型转换");
                //3.打开对话框
                transformDialog.dialog("open");
                //4.回显数据
                transformForm.form("load",rowData);//基于同名匹配规则
            }else{
                $.messager.alert("温馨提示","请选择一条需要查询的数据.","warning");
            }
        },
        transformSave:function () {
            var url;
            var rowData = checkDataGrid.datagrid("getSelected");
            console.log(rowData);
            if (rowData.c_paymentType=='刷卡缴费') {
                url = "/check/slotCardUpdate";
            } else {
                url = "/check/cheque";
            }
            transformForm.form("submit",{
                url:url,
                success:function(data){
                    data = $.parseJSON(data);
                    console.log(data);
                    if(data.success){
                        //提示消息,当点确定的时候,关闭对话框,刷新数据表格
                        $.messager.alert("温馨提示",data.msg,"info",function(){
                            transformDialog.dialog("close");
                            checkDataGrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            });
        },
        issueSendBack:function () {
            var rowData = checkDataGrid.datagrid("getSelected");
            console.log(rowData);
            if(rowData){
                $.messager.confirm("温馨提示","您确定需要退回该数据吗?",function(yes){
                    if(yes){
                        $.get("/check/issueSendBack?id="+rowData.id+"&orderBillId="+rowData.c_orderBill_id,function(data){
                            if(data.success){
                                checkDataGrid.datagrid("reload");
                                $.messager.alert("温馨提示",data.msg,"info");
                            }else{
                                $.messager.alert("温馨提示",data.msg,"error");
                            }
                        },"json")
                    }
                });
            }else{
                $.messager.alert("温馨提示","请选择需要退回的数据.","warning");
            }
        },
        check:function () {
            var rowData = checkDataGrid.datagrid("getSelected");
            if(rowData){
                if (rowData.c_paymentType=='支票缴费') {
                    //1.清空表单数据
                    checkForm.form("clear");
                    //2.设置对话框的标题
                    checkDialog.dialog("setTitle","支票收付信息");
                    //3.打开对话框
                    checkDialog.dialog("open");
                    //4.回显数据
                    checkForm.form("load",rowData);//基于同名匹配规则
                    //回显角色信息.
                    //[1,3]----->List<Long>
                    bankAreaId.combobox({
                        onSelect: function (row) {
                            console.log(row);
                            if (row != null) {
                                provinceId.combobox({
                                    url:"/bankarea/queryChildrenByParentId?parentId="+row.id
                                });
                            }
                        }
                    });
                    provinceId.combobox({
                        onSelect: function (row) {
                            console.log(row);
                            if (row != null) {
                                cityId.combobox({
                                    url:"/bankarea/queryChildrenByParentId?parentId="+row.id
                                });
                            }
                        }
                    });
                }else{
                    $.messager.alert("温馨提示","请选择一条支票缴费的数据.","warning");
                }
            }else{
                $.messager.alert("温馨提示","请选择一条需要修改的数据.","warning");
            }
        },
        slotCard:function () {
            var rowData = checkDataGrid.datagrid("getSelected");
            if(rowData){
                if (rowData.c_paymentType=='刷卡缴费') {
                    //1.清空表单数据
                    slotCardForm.form("clear");
                    //2.设置对话框的标题
                    slotCardDialog.dialog("setTitle","刷卡缴费信息");
                    //3.打开对话框
                    slotCardDialog.dialog("open");
                    //4.回显数据
                    slotCardForm.form("load",rowData);//基于同名匹配规则
                    //回显角色信息.
                    //[1,3]----->List<Long>
                    bankAreaId1.combobox({
                        onSelect: function (row) {
                            console.log(row);
                            if (row != null) {
                                provinceId1.combobox({
                                    url:"/bankarea/queryChildrenByParentId?parentId="+row.id
                                });
                            }
                        }
                    });
                    provinceId1.combobox({
                        onSelect: function (row) {
                            console.log(row);
                            if (row != null) {
                                cityId1.combobox({
                                    url:"/bankarea/queryChildrenByParentId?parentId="+row.id
                                });
                            }
                        }
                    });
                }else{
                   $.messager.alert("温馨提示","请选择一条刷卡缴费的数据.","warning");
                }
            }else{
                $.messager.alert("温馨提示","请选择一条需要修改的数据.","warning");
            }
        },
        slotCardSave:function () {
            slotCardForm.form("submit",{
                url:"/check/update",
                onSubmit:function(param){
                    param['orderBill.id']=$("#slotCardId").val();
                    return true;
                },
                success:function(data){
                    data = $.parseJSON(data);
                    console.log(data);
                    if(data.success){
                        //提示消息,当点确定的时候,关闭对话框,刷新数据表格
                        $.messager.alert("温馨提示",data.msg,"info",function(){
                            slotCardDialog.dialog("close");
                            checkDataGrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            });
        },
        save:function () {
            checkForm.form("submit",{
                url:"/check/update",
                onSubmit:function(param){
                    param['orderBill.id']=$("#checkId").val();
                    return true;
                },
                success:function(data){
                    data = $.parseJSON(data);
                    console.log(data);
                    if(data.success){
                        //提示消息,当点确定的时候,关闭对话框,刷新数据表格
                        $.messager.alert("温馨提示",data.msg,"info",function(){
                            checkDialog.dialog("close");
                            checkDataGrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            });
        },
        issue:function () {
            var rowData = checkDataGrid.datagrid("getSelected");
            if(rowData){
                //1.清空表单数据
                issueForm.form("clear");
                //2.设置对话框的标题
                issueDialog.dialog("setTitle","见费出单信息");
                //3.打开对话框
                issueDialog.dialog("open");
                //4.回显数据
                issueForm.form("load",rowData);//基于同名匹配规则
            }else{
                $.messager.alert("温馨提示","请选择一条需要查询的数据.","warning");
            }
        },
        cancel:function(){
            checkDialog.dialog("close");
        },
        slotCardCancel:function(){
            slotCardDialog.dialog("close");
        },
        issueCancel:function(){
            issueDialog.dialog("close");
        },
        transformCancel:function(){
            transformDialog.dialog("close");
        },
        reload:function(){
            orderBillForm.form("clear");
            //刷新数据表格
            checkDataGrid.datagrid("reload");
        }
    }
});
function statusFormatter(value,record,index){
    if(value==4){
        return "<font color='red'>待缴费</font>";
    }else if(value==5){
        return "<font color='green'>缴费成功</font>";
    }
}